home comics writing pictures archive about

Set.cpp

Language: C++
Last Modified: 2022-09-10 5:29:28 PM UTC
File Size: 4729 bytes
http://www.penguinstew.ca/example/CodeFormater/Set.cpp
#include "Set.h"
#include <sstream>
#include <regex>
#include "PhpHelper.h"
#include "FormatHelper.h"
#define SET_COLOUR_ATTRIBUTE "colour"
#define SET_REGEX_ATTRIBUTE "regEx"
#define SET_WILD_REGEX_ATTRIBUTE "wildRegEx"
#define SET_STARTTYPES_ELEMENT "startTypeIds"
#define SET_STARTTYPE_ELEMENT "startType"
#define SET_WORDS_ELEMENT "words"
const std::string Set::SET_PATTERN_WORD = "~word";
#define SET_PATTERN_WORD_LENGTH 5
Set::Set(xmlNodePtr xmlNode)
{
colour = "";
regExText = "a^";
regEx = std::regex(regExText);
wildRegExText = "[a-zA-Z0-9]";
wildRegEx = std::regex(wildRegExText);
words = std::vector<std::string>();
startIds = std::vector<TypeIdPair>();
xmlChar* xmlString;
xmlNodePtr xmlChild = NULL;
xmlNodePtr xmlSubChild = NULL;
xmlString = xmlGetProp(xmlNode, xmlCharStrdup(SET_COLOUR_ATTRIBUTE));
if (xmlString != NULL)
{
colour = std::string((char*)xmlString);
}
xmlString = xmlGetProp(xmlNode, xmlCharStrdup(SET_REGEX_ATTRIBUTE));
if (xmlString != NULL)
{
regExText = std::string((char*)xmlString);
regEx = std::regex(regExText);
}
xmlString = xmlGetProp(xmlNode, xmlCharStrdup(SET_WILD_REGEX_ATTRIBUTE));
if (xmlString != NULL)
{
wildRegExText = std::string((char*)xmlString);
wildRegEx = std::regex(wildRegExText);
}
for (xmlChild = xmlNode->children; xmlChild != NULL; xmlChild = xmlChild->next)
{
if (xmlChild->type != XML_ELEMENT_NODE) {
continue;
}
if (xmlStrEqual(xmlChild->name, xmlCharStrdup(SET_STARTTYPES_ELEMENT)))
{
for (xmlSubChild = xmlChild->children; xmlSubChild != NULL; xmlSubChild = xmlSubChild->next)
{
if (xmlChild->type != XML_ELEMENT_NODE) {
continue;
}
if (xmlStrEqual(xmlSubChild->name, xmlCharStrdup(SET_STARTTYPE_ELEMENT)))
{
TypeIdPair typePair = TypeIdPair(xmlSubChild);
startIds.push_back(typePair);
}
}
}
if (xmlStrEqual(xmlChild->name, xmlCharStrdup(SET_WORDS_ELEMENT)))
{
xmlString = xmlNodeGetContent(xmlChild);
if (xmlString != NULL)
{
words = PhpHelper::explode(" ",std::string((char*)xmlString));
}
}
}
}
std::string Set::GetColour()
{
return this->colour;
}
std::regex Set::GetRegEx()
{
return this->regEx;
}
std::regex Set::GetWildRegEx()
{
return this->wildRegEx;
}
std::vector<std::string> Set::GetWords()
{
return this->words;
}
std::vector<TypeIdPair> Set::GetStartIds()
{
return startIds;
}
int Set::FindAndPrintSetWord(std::stringstream& lineStream, std::string line, int pos, TypeIdPair testType, char preC, State currentState)
{
if (std::regex_match(std::string(1, preC), regEx))
{
return 0;
}
bool match = false;
for (unsigned int i = 0; i < startIds.size(); i++)
{
TypeIdPair ti = startIds.at(i);
if (ti.IsMatch(testType))
{
match = true;
break;
}
}
if (!match)
{
return 0;
}
std::string word;
std::string wildCardWord;
std::string foundWord;
for (unsigned int i = 0; i < words.size(); i++)
{
word = words.at(i);
size_t wildcardPos = word.find(SET_PATTERN_WORD);
if (wildcardPos != std::string::npos)
{
if (wildcardPos != 0)
{
wildCardWord = word.substr(0, wildcardPos);
if (FormatHelper::IsMatch(line, wildCardWord, pos, regEx))
{
wildCardWord.append(FormatHelper::GetWord(line, pos + wildcardPos, wildRegEx));
foundWord = wildCardWord;
break;
}
}
else
{
foundWord = FormatHelper::GetWord(line, pos, regEx);
break;
}
}
if (FormatHelper::IsMatch(line, word, pos, regEx))
{
foundWord = word;
break;
}
}
int wordLength = foundWord.length();
if (wordLength > 0)
{
if (currentState.GetId() != 0)
{
currentState.PrintClose(lineStream);
}
std::string cleanWord = PhpHelper::htmlspecialchars(foundWord);
lineStream << "<span class=\"" << colour << "\">" << cleanWord << "</span>";
if (currentState.GetId() != 0)
{
currentState.PrintRestart(lineStream);
}
return wordLength;
}
return 0;
}
std::string Set::ToString()
{
std::stringstream stream;
stream << "Set: ";
stream << "Colour: " << this->colour << ", ";
stream << "RegEx: " << this->regExText << std::endl;
stream << "WildRegEx: " << this->wildRegExText << std::endl;
stream << "--- Words ---" << std::endl;
for (unsigned int i = 0; i < words.size(); i++)
{
stream << words.at(i) << std::endl;
}
stream << "--- Start IDs ---" << std::endl;
for (unsigned int i = 0; i < startIds.size(); i++)
{
TypeIdPair pair = startIds.at(i);
stream << pair.ToString() << std::endl;
}
return stream.str();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208